<?php

class xmlGenerator 
{
//============= Using DOM for parsing  XML  ========== //
	function process_children($node)
	{
		$children = $node->childNodes;
		foreach ($children as $elem) {
			if ($elem->nodeType == XML_TEXT_NODE) {
				if (strlen(trim($elem->nodeValue))) {
					echo trim($elem->nodeValue)."\n";
				}
			} else if ($elem->nodeType == XML_ELEMENT_NODE) {
				process_children($elem);
			}
			//=========== OR =========== //
			if ($elem->nodeType == XML_ELEMENT_NODE) {
				 if ($elem->nodeName == 'body') {
				 echo $elem->getAttributeNode('background')->value. "\n";
				 }
				 process_children($elem);
				 }
				 }
		}
	}
//============= Write& Read functions for XML Using DOM ========== //
	function Dom_writeXmL(){
		$dom = new DomDocument();
		$html = $dom->createElement('html');
		$html->setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
		$html->setAttribute("xml:lang", "en");
		$html->setAttribute("lang", "en");
		$dom->appendChild($html);
		
		$head = $dom->createElement('head');
		$html->appendChild($head);
		$title = $dom->createElement('title');
		$title->appendChild($dom->createTextNode("XML Example"));
		$head->appendChild($title);
		
		/* Create the body element */
		$body = $dom->createElement('body');
		$body->setAttribute("backgound", "bg.png");
		$html->appendChild($body);
		/* Create the p element */
		$p = $dom->createElement('p');
		$body->appendChild($p);
		
		/* Add the "Moved to" */
		$text = $dom->createTextNode("Moved to ");
		$p->appendChild($text);
		/* Add the a */
		$a = $dom->createelement('a');
		$a->setAttribute("href", "http://example.org/");
		$a->appendChild($dom->createTextNode("example.org"));
		$p->append_child($a);
		/* Add the ".", br and "foo & bar" */
		$text = $dom->createTextNode(".");
		$p->appendChild($text);
		$br = $dom->createElement('br');
		$p->appendChild($br);
		$text = $dom->createTextNode("foo & bar");
		$p->appendChild($text);
		
		echo $dom->saveXML();
		
		/*  ==== Output
			<?xml version="1.0"?>
			<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
			<head>
			<title>XML Example</title>
			</head>
			<body background="bg.png">
			<p>Moved to <a href="http://example.org/">example.org</a>.
			?<br>foo &amp; bar</p>
			</body>
			</html>
		*/
	} 
	function Dom_readXml()
	{
		$dom = new DomDocument();
		$dom->load('test2.xml');
		$root = $dom->documentElement;

		process_children($root);
		//=========== or ========== /
		$dom = new DomDocument();
		 $dom->load('test2.xml');
		 $body = $dom->documentElement->getElementsByTagName('body')->item(0);
		 echo $body->getAttributeNode('background')->value. "\n";
		 // =========== Using XPath ========= //
		 $dom = new DomDocument();
		 $dom->load('test2.xml');
		 $xpath = new DomXPath($dom);
		 $nodes = $xpath->query("*[local-name()='body']", $dom->documentElement);
		 echo $nodes->item(0)->getAttributeNode('background')->value."\n";
	}
//============= Using SimpleXML library for parsing  XML  ========== //
	function simpleXml(){
		$sx1 = simplexml_load_file('example.xml');
		// OR  === $sx2 = simplexml_load_string($string);
		// OR  === $sx3 = simplexml_load_dom(new DomDocument());
		foreach ($sx2->body->p as $p) {
			echo $sx->body->p[1];
			echo $sx->body['background'];
			echo strip_tags($sx->body->p[1]->asXML()) . "\n";
		}
		//=========================== Storing SimpleXML Objects 
		file_put_contents('filename.xml', $sx2->asXML());
	}
 

}

?>